'''
Project:      World Exposition
Name:         redneck.py
Purpose:      a playable character

Authors:      Sam Milender, Calvin Berger
Main Author:  Sam Milender

Created:      4/30/13
Copyright:    (c) Sam Milender 2013
Licence:      New BSD
-----------------------------------------------------------------------------
'''
import pygame, math, sys, random, time

class Player(pygame.sprite.Sprite):
    def __init__(self, speed, position, screenSize, image):
        pygame.sprite.Sprite.__init__(self, self.containers)
        self.images = []
        self.images = [pygame.image.load(image)]
        self.frame = 0
        self.maxFrame = len(self.images)-1
        self.image = self.images[self.frame]
        self.rect = self.image.get_rect()
        self.speed = [0,0]
        self.XmaxSpeed = 6
        self.YmaxSpeed = 4.5
        self.realx = position[0]
        self.realy = position[1]
        self.x = position[0]
        self.y = position[1]
        self.place(position)
        self.living = True
        self.didHit = False
        self.screenSize = self.screenWidth, self.screenHeight = screenSize
        self.scrollingX = False
        self.scrollingY = False
        self.scrollThreshold = 200
        self.health = 20
        self.dir = "right"

    def  __str__(self):
        pass
#        return "I'm a player " + str(self.rect.center) + str(self.speed) + str(self.living)
     
    def place(self, position):
        self.rect.center = position
        #print "I've moved to", position
        
    def move(self):
        self.realx += self.speed[0]
        self.x += self.speed[0]
        if self.x >= self.screenWidth - self.scrollThreshold:
            self.x = self.screenWidth - self.scrollThreshold
            self.scrollingX = True
        elif self.x <= self.scrollThreshold:
            self.x = self.scrollThreshold
            self.scrollingX = True
        else:
            self.scrollingX = False

        self.realy += self.speed[1]
        self.y += self.speed[1]
        if self.y >= self.screenHeight - self.scrollThreshold:
            self.y = self.screenHeight - self.scrollThreshold
            self.scrollingY = True
        elif self.y <= self.scrollThreshold:
            self.y = self.scrollThreshold
            self.scrollingY = True
        else:
            self.scrollingY = False  
        
        self.place([self.x, self.y])
        
    def distToPoint(self, pt):
        x1 = self.rect.center[0]
        x2 = pt[0]
        y1 = self.rect.center[1]
        y2 = pt[1]
        return math.sqrt(((x2-x1)**2)+((y2-y1)**2))
        
    def direction(self, dir):
        if dir == "left":
            self.speed = [-self.XmaxSpeed, 0]
            self.dir = "left"
        elif dir == "right":
            self.speed = [self.XmaxSpeed, 0]
            self.dir = "right"
        elif dir == "up":
            self.speed = [0, -self.YmaxSpeed]
            self.dir = "up"
        elif dir == "down":
            self.speed = [0, self.YmaxSpeed]
            self.dir = "down"
        elif dir == "stop":
            self.speed = [0,0]
   
    def fenceCollide(self, fence):
        print "Fence detected me!! Hee Hee"
        if (self.rect.right > fence.rect.left 
        and self.rect.left < fence.rect.right):
            if (self.rect.bottom > fence.rect.top and 
                self.rect.top < fence.rect.bottom):
                    self.speed[0] = self.speed[0] * -1
                    self.speed[1] = self.speed[1] * -1
                    self.move()
                    self.move()
                    self.speed = [0,0]
            
    def enemyCollide(self, other):
        other.speed[0] = other.speed[0] * -1
        other.speed[1] = other.speed[1] * -1
        self.health = self.health - 10
        if self.health == 0:
            self.living = False
        
    
    def update(*args):
        self = args[0]
        self.xOffset = args[1]
        self.didHit = False
        if not self.living:
            self.kill()
        self.move()


